<?php

namespace catchAdmin\pc\model;

use catchAdmin\model\NeedMatchUser;
use catchAdmin\model\Notice;
use catcher\exceptions\AlertErrorException;

class Need extends \catchAdmin\model\Need
{


    public $name = 'need';


    public function matchUser($param)
    {
        $this->startTrans();

        // 1.需求变审核通过

        $need_msg = (new \catchAdmin\model\Need())
            ->where('id', $param['need_id'])
            ->find();
        if ($need_msg['status'] == 3) {
            throw new AlertErrorException('已拒绝，不能匹配');
        }
        $need_msg->status = 2;
        $need_msg->stage = 2;
        $need_msg->save();

        $arr_save = [];
        foreach ($param['user_company_id'] as $k => $v) {
            $arr_save[] = $v;
        }

        $NeedMatchUser_app = (new NeedMatchUser());

        $NeedMatchUser_app
            ->where('need_id', $param['need_id'])
            ->whereNotIn('user_company_id', $arr_save)
            ->where('deleted_at', 0)
            ->update([
                'deleted_at' => time(),
            ]);

        foreach ($param['user_company_id'] as $k => $v) {
            if ($NeedMatchUser_app
                    ->where('need_id', $param['need_id'])
                    ->where('user_company_id', $v)
                    ->count() > 0) {
                continue;
            }

            $user_company_msg = (new \catchAdmin\model\UserCompany())
                ->where('id', $v)
                ->find();

            $NeedMatchUser_app->saveDataCC([
                'need_id'         => $param['need_id'],
                'user_company_id' => $v,
                'wechat_user_id'  => $user_company_msg['wechat_user_id'],
            ]);
        }

        $this->commit();

        return true;
    }


    public function indexData($param)
    {
        $app = $this;

        if (!empty($param['company_type'])) {
            $app = $app->where('company_type', $param['company_type']);
        }

        return $this->indexDataPcCC($param, $app);
    }


    /**
     * 审核
     */
    public function audit($param)
    {
        $need_msg = $this
            ->where('id', $param['id'])
            ->find();
        if ($need_msg->status != 1) {
            throw new AlertErrorException('不能重复审核');
        }

        $this->startTrans();

        // 1.需求表
        $need_msg->status = $param['status'];
        $need_msg->status_note = $param['status_note'] ?? '';
        $need_msg->status_time = time();
        if ($need_msg->status == 2) {
            $need_msg->publish_time = time();
        }

        if ($need_msg->status == 2) {
            $need_msg->stage = 2;
        } elseif ($need_msg->status == 3) {
            $need_msg->stage = 3;
        }
        $need_msg->save();

        // 2.发消息
        (new Notice())->saveDataCC([
            // 需求审核——审核结果
            'notice_type'    => 3,
            'title'          => $need_msg['status_name'],
            'content'        => $need_msg['status_note'],
            'wechat_user_id' => $need_msg['wechat_user_id'],
        ]);

        $this->commit();

        return true;
    }
}
